package mo.leetcode_hot;

import mo.common.ListNode;

public class L2 {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int add = 0;
        ListNode res = new ListNode(-1);
        ListNode cur = res;
        while (l1 != null && l2 != null) {
           int sum = l1.val + l2.val + add;
           add = sum / 10;
           sum = sum % 10;
           cur.next = new ListNode(sum);
           cur = cur.next;
           l1 = l1.next;
           l2 = l2.next;
        }
        while (l1 != null) {
            int sum = l1.val + add;
            add = sum / 10;
            sum = sum % 10;
            cur.next = new ListNode(sum);
            cur = cur.next;
            l1 = l1.next;
        }
        while (l2 != null) {
            int sum = l2.val + add;
            add = sum / 10;
            sum = sum % 10;
            cur.next = new ListNode(sum);
            cur = cur.next;
            l2 = l2.next;
        }
        if (add != 0) {
            cur.next = new ListNode(add);
        }
        return res.next;
    }



}
